package com.ruoyi.third.domain;

import com.ruoyi.common.annotation.Excel;
import javax.validation.constraints.NotNull;

import com.ruoyi.common.utils.StringUtils;
import lombok.Data;
import com.ruoyi.common.core.domain.BaseEntity;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * 接入平台对象 third_platform
 * 
 * @author yepanpan
 * @date 2025-02-06
 */
@Data
public class ThirdPlatform extends BaseEntity
{
    private static final long serialVersionUID = 1L;

    /** 自增长主键ID */
    private Long id;

    /** 代码 */
    @Excel(name = "代码")
    @NotNull
    private String code;

    /** 名称 */
    @Excel(name = "名称")
    @NotNull
    private String name;

    /** IP */
    @Excel(name = "IP")
    private String ip;

    /** 端口 */
    @Excel(name = "端口")
    private Integer port;

    /** 心跳接口 */
    @Excel(name = "心跳接口")
    private String apis;

    /** 状态 */
    @Excel(name = "状态", dictType="business_status", comboReadDict=true)
    @NotNull
    private String status;

    /** 运行 */
    @Excel(name = "运行", dictType="run_status", comboReadDict=true)
    private String running;

    private Map<String, String> configMap = new HashMap<>();

    public void setConfig(List<ThirdConfigField> configFieldList){
        if(configFieldList == null || configFieldList.isEmpty()){
            return;
        }
        for(ThirdConfigField configField:configFieldList){
            if(StringUtils.isEmpty(configField.getFieldValue())){
                configMap.put(configField.getFieldCode(), configField.getFieldValue());
            }else{
                configMap.put(configField.getFieldCode(), configField.getDefaults());
            }
        }
    }

    /**
     * 查询指定配置
     * @param fieldCode
     * @return
     */
    public String getConfig(String fieldCode){
        return configMap.get(fieldCode);
    }

    /**
     * 查询指定配置的整数值
     * @param fieldCode
     * @return
     */
    public Integer getConfigInt(String fieldCode, Integer def){
        String v= configMap.get(fieldCode);
        if(StringUtils.isEmpty(v)){
            return def;
        }

        try {
            return Integer.parseInt(v);
        }catch (Exception e){
            return def;
        }
    }

    /**
     * 查询指定配置是否为真：YES|Y|1
     * @param fieldCode
     * @return
     */
    public Boolean getConfigBoolean(String fieldCode, Boolean def){
        String v = configMap.get(fieldCode);
        if(StringUtils.isEmpty(v)){
            return def;
        }

        return "Y".equalsIgnoreCase(v) || "YES".equals(v) || "1".equalsIgnoreCase(v);
    }
}
